<template>
  <div class="assignRole">
    <Modal v-model="isAssign"
           :mask-closable="false"
           :title="'授权：'+user.nickName">
      <div slot="footer">
        <Button type="text"
                size="large"
                @click="cancel">取消</Button>
        <Button type="primary"
                size="large"
                @click="ok">确定</Button>
      </div>
      <Form ref="user"
            :model="user"
            :label-width="80"
            :rules="rules">
        <FormItem label="账号">
          <Input v-model="user.loginName"
                 disabled />
        </FormItem>
        <FormItem label="用户名">
          <Input v-model="user.userName"
                 disabled />
        </FormItem>
        <FormItem label="密码:"
                  v-if="!user.salt">
          <!--<Input type="password" v-model="user.password" />-->
          初始密码为123456，请用户自行登录修改!
        </FormItem>
        <FormItem label="角色:">
          <RadioGroup v-model="user.roleId">
            <Radio :label="role.id"
                   v-for="(role) in roleLists"
                   :key="role.id">{{role.name}}</Radio>
          </RadioGroup>
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>

<script>
export default {
  name: "assignRole",
  data () {
    return {
      isAssign: false,
      roleLists: [],
      selectRoles: [],
      user: {},
      rules: {
      }
    }
  },
  methods: {
    cancel () {
      this.isAssign = false;
    },
    openAssignRoleView (row) {
      this.user = Object.assign({}, row)
      this.selectRoles = []
      this.isAssign = true
      this.getRoleList()
    },
    getRoleList () {
      this.ajax({
        type: this.api.roleList.type,
        url: this.api.roleList.url,
        data: {},
        success: (data) => {
          this.roleLists = data.object
        },
        error: (data) => {
          this.$Message.error(data.message);
        }
      })
    },
    ok () {
      this._setRole()
    },
    _setRole () {
      let user = Object.assign({}, this.user)

      this.ajax({
        type: this.api.setUserRole.type,
        url: this.api.setUserRole.url,
        data: {
          'adminId': this.user.id,
          'roleId': this.user.roleId
        },
        success: (data) => {
          this.isAssign = false
          this.$emit('getList')
        },
        error: (data) => {
          this.$Message.error(data.message);
        }
      })
    },
  }
}
</script>

<style lang="scss" type="text/scss" rel="stylesheet/scss">
</style>
